<template>
  <div>
    <el-row>
      <el-col :span="24" class="addRole">
        <!-- 查询 -->
        <el-popover placement="bottom-start" width="1000" style="margin-right:10px;" trigger="click">
          <el-form :inline="true" :model="search">
              <el-form-item label="图纸编号" >
                  <el-input v-model="search.drawCode"></el-input>
              </el-form-item>
              <el-form-item label="生产令号" >
                  <el-input v-model="search.makeNo"></el-input>
              </el-form-item>
              <el-form-item label="自定义编码" >
                  <el-input v-model="search.code"></el-input>
              </el-form-item>
              <el-button type="primary" @click="searchBtn">确认</el-button>
          </el-form>
          <el-button slot="reference" type="primary" icon="el-icon-search">查询</el-button>
        </el-popover>
        <el-button type="primary" @click="showExcelDialog()">计划导入</el-button>
      </el-col>
    </el-row>
    <!-- 29所列表 -->
    <tableList29 ref="tableList29" :postData="postData">
      <template v-slot:default="scope">
        <el-button @click="showDialog('update',scope.row)" type="text" size="small">详情</el-button>
        <!-- <el-button @click="showDialog('print',scope.row)" type="text" size="small">工艺流程卡</el-button> -->
      </template>
    </tableList29>
    <!--计划任务详情-->
    <el-dialog class="bodyNopaddingTop" :title="dialog.title" :visible.sync="dialog.isShow"  v-dialogDrag width="1100px" @close='closeDialog'>
      <el-tabs v-model="dialog.activeName" @tab-click="dialogHandleClick">
        <el-tab-pane label="详情" name="1">
          <taskDetail29 :detailObj="dialog.formData" :type="dialog.type" @editSuccess="editSuccess" :editDisalbe="false"></taskDetail29>
        </el-tab-pane>
        <el-tab-pane label="29所工艺" name="2" v-if="dialog.title==='计划详情'">
          <!-- <el-button type="primary" @click="showDialog1('insert')">新增</el-button> -->
          <el-table :data="dialog.tableData" stripe border class="user-table" style="width: 100%;"> 
            <el-table-column fixed type="index" label="*" sortable min-width="30" align="center"></el-table-column>
            <el-table-column prop="processNo" label="工序号" sortable  min-width="30" align="center"></el-table-column>
            <el-table-column prop="processName" label="工序名称" sortable min-width="80" align="center"></el-table-column>
            <el-table-column prop="processTime" label="工时" sortable min-width="80"></el-table-column>
            <el-table-column prop="processPrice" label="外包工序工时/面积/单独报价" sortable min-width="80"></el-table-column>
            <el-table-column prop="nums" label="件数" sortable width="60"></el-table-column>
            <el-table-column prop="priority" label="优先级" sortable width="60"></el-table-column>
            <el-table-column prop="planCompleteDate" label="计划完成日期" sortable min-width="100"  :formatter="timeFormatter"></el-table-column>
            <el-table-column prop="startTime" label="任务开始时间" sortable min-width="100"  :formatter="timeFormatter"></el-table-column>
            <el-table-column prop="creatorName" label="创建人" sortable width="100"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" sortable width="110" :formatter="timeFormatter"></el-table-column>
            <el-table-column fixed="right" label="操作" align="center" min-width="80">
              <template slot-scope="scope">
                <el-button @click="showDialog1('update',scope.row)" type="text" size="small">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="图纸上传" name="3" v-if="dialog.title==='计划详情'">
          <el-upload class="upload-demo" ref="drawingUp" :action="pdfActionUrl" :before-upload="beforeAvatarUpload" :data="dialog.uploadData" :on-success="handleAvatarSuccess"  multiple :limit="1">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
           <uploadList :upLoadType="1" :drawNo="dialog.drawCode"  ref="drawing"></uploadList>
        </el-tab-pane>
        <el-tab-pane label="工艺上传" name="4" v-if="dialog.title==='计划详情'">
          <el-upload class="upload-demo" ref="technologyUp" :action="pdfActionUrl" :before-upload="beforeAvatarUpload" :data="dialog.uploadData" :on-success="handleAvatarSuccess"  multiple :limit="1">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
           <uploadList :upLoadType="2" :drawNo="dialog.drawCode"  ref="technology"></uploadList>
        </el-tab-pane>
        <el-tab-pane label="图形文件上传" name="5" v-if="dialog.title==='计划详情'">
          <el-upload class="upload-demo" ref="graphicsFileUp" :action="pdfActionUrl" :before-upload="beforeAvatarUpload" :data="dialog.uploadData"  :on-success="handleAvatarSuccess"  multiple :limit="1">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
           <uploadList :upLoadType="3" :drawNo="dialog.drawCode"  ref="graphicsFile"></uploadList>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    
  <!-- 新增编辑29所计划任务详情 -->
    <el-dialog class="bodyNopaddingTop" :title="dialog1.title" :visible.sync="dialog1.isShow" v-dialogDrag>
      <processPlanDetail29 :formData="dialog1.formData" :type="dialog1.type" :tid="dialog.formData.tid" @editSuccess="editSuccess1"></processPlanDetail29>
    </el-dialog>
    <!-- Excel 导入弹框-->
    <!-- <el-dialog class="bodyNopaddingTop" :title="dialog4.title" :visible.sync="dialog4.isShow" v-dialogDrag>
      <el-upload class="upload-excel text-center" drag :action="dialog4.excelActionUrl" multiple :limit="1" ref="upload"
      :before-upload="beforeAvatarUpload1" 
      :on-success="handleAvatarSuccess1"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传Excel文件</div>
      </el-upload>
    </el-dialog> -->
    <!-- 打印 -->

  </div>
</template>
<script>
import pdf from 'vue-pdf';
import planApi from 'plugins/axios/planApi.js';
import baseApi from 'plugins/axios/baseApi.js';
import uploadList from 'views/components/uploadList.vue' /**1-图纸上传、2-工艺上传、3-图形文件上传 的列表组件 */
import tableList29 from 'views/components/tableList29.vue' /**29所列表组件 */
import taskDetail29 from 'views/components/taskDetail29.vue' /**29所任务详情组件 */
import processPlanDetail29 from 'views/components/processPlanDetail29.vue' /**29所工序详情组件 */

export default {
  components: {
    pdf,
    uploadList,
    tableList29,
    taskDetail29,
    processPlanDetail29
  },
  data(){
    return {
      status:'0',
      postData:{
        isMaterialArrvie:0,
        isMaterialTailor:0
      },
      machineAllList:[],/**所有机器列表 */
      search:{
        drawCode:"",
        makeNo:"",
        code:""
      },
      dialog:{/**弹框配置 */
        title:'',
        isShow:false,
        type:'',
        formData:{},
        activeName:'2',
        otherUpDataData:{},
        uploadData:{
          drawInfo:"",
        },/**上传图纸需要额外提交的数据 */
        tableData:[],
        drawCode:''
      },
      pdfActionUrl: baseApi.drawInfoUpload(),
      dialog1:{
        title:'',
        type:'',
        isShow:false,
        formData:{}
      },
      /**excel导入弹框*/ 
      dialog4:{
        title:'',
        isShow:false,
        excelActionUrl:baseApi.excelUpload()
      }
    }
  },
  created(){},
  mounted(){
    this.status = this.$route.query.status;
    this.getTablList();
  },
  watch: {
    $route(){
      this.status = this.$route.query.status;
      this.search.drawCode="";
      this.search.makeNo="";
      this.search.code="";
      this.getTablList();
    }
  },
  methods:{
    cellClassName({row, column, rowIndex, columnIndex}){
      if(columnIndex>0&&columnIndex<6){
        return 'nopadding'
      }
    },
    /**时间格式化 */
    timeFormatter(row, column, cellValue, index){
      return cellValue?this.timestampToTime(cellValue,'yyyy-mm-dd'):'';
    },
    /**29所计划列表 */
    getTablList(){
      let self = this;
      self.postData.drawCode=self.search.drawCode;
      self.postData.makeNo=self.search.makeNo;
      self.postData.code=self.search.code;
      self.postData.isUploadDraw=self.status;
      self.$refs.tableList29.get29TableList()
    },
    searchBtn(){
      this.getTablList();
    },
    showDialog(type,row){
      this.dialog.activeName='2';
      this.dialog.type =type;
      switch(type){
        case 'insert':
          this.dialog.isShow=true;
          this.dialog.formData={};
          this.dialog.title = '新增计划任务';
          break;
        case 'update':
          this.dialog.isShow=true;
          this.dialog.formData = row;
          this.dialog.title = '计划详情';
          this.dialog.drawCode = row.drawCode; 
          this.dialog.otherUpDataData.drawNo=row.drawCode;
          this.getPlanTaskDetailList();
          break;
        case 'print':
           this.dialog.formData = row;
           this.$refs.print1.getCgPdfData(row.id);
          break;
      } 
    },
    showDialog1(type,row){
      this.dialog1.isShow=true;
      this.dialog1.type=type;
      switch(type){
        case 'insert':
          this.dialog1.formData={};
          this.dialog1.title = '新增29所工艺详情';
          break;
        case 'update':
          this.dialog1.formData = row;
          this.dialog1.title = '编辑29所工艺详情';
          break;
      }
    },
    /**修改成功后更新表格关闭弹框*/ 
    editSuccess(){
      this.dialog.isShow=false;
      this.getTablList();
      this.$message.success(`${this.dialog.title}成功`);
    },
    editSuccess1(){
      this.dialog1.isShow=false;
      this.$message.success(`${this.dialog1.title}成功`)
      this.getPlanTaskDetailList();
    },
    onSubmit(formName){
      let self = this;
      self.$refs[formName].validate((valid) => {
          if (valid) {
            switch(formName){
              case 'ruleForm1':
                if(self.dialog1.type==='insert'){
                  self.dialog1.formData.tid=self.dialog.formData.id;
                }
                planApi.insertUpdatePlanTask(self.dialog1.type,self.dialog1.formData).then(res=>{
                  self.dialog1.isShow=false;
                  self.$message({
                    message: `${self.dialog1.title}成功`,
                    type: 'success'
                  });
                  self.getPlanTaskDetailList();
                })
                break;
            }  
          }
        });
    },
    /**详情切换tab*/ 
    dialogHandleClick(){
      let self = this;
      switch(self.dialog.activeName){
        case '2':
          self.getPlanTaskDetailList();
          break;
         case '3':
           self.$refs.drawing.getTableList();
           self.dialog.otherUpDataData.type=1;
           self.dialog.uploadData.drawInfo = JSON.stringify(self.dialog.otherUpDataData);
          break;
         case '4':
           self.$refs.technology.getTableList();
           self.dialog.otherUpDataData.type=2;
           self.dialog.uploadData.drawInfo = JSON.stringify(self.dialog.otherUpDataData);
          break;
        case '5':
          self.$refs.graphicsFile.getTableList();
          self.dialog.otherUpDataData.type=3;
          self.dialog.uploadData.drawInfo = JSON.stringify(self.dialog.otherUpDataData);
          break;
      }
    },
    /**获取任务详情列表*/ 
    getPlanTaskDetailList(){
        let self = this;
        planApi.planTaskDetailList1(self.dialog.formData.id).then(res=>{
        self.dialog.tableData=res.content;
      })
    },
    beforeAvatarUpload(file){/**文件上传前格式和大小的验证*/
      console.log(file);
      return true;
    },
    /**计划导入之前格式的判断 */
    beforeAvatarUpload1(file){
      const isExcel = (file.type=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")||(file.type == 'application/vnd.ms-excel');
      if(!isExcel){
        this.$message.error('只能上传Excel文件!');
      }
      return isExcel;
    },
    /**计划导入后执行的方法*/
    handleAvatarSuccess1(response, file, fileList){
     if(response.statusCode==="200"){
        this.dialog4.isShow = false;
        this.$message.success('上传成功');
        this.getTablList();
     }else{
       this.$message.error(response.desc);
       this.$refs.upload.clearFiles(); 
     } 
    },
    /**图纸上传、工艺上传、图形文件上传后执行的方法**/ 
    handleAvatarSuccess(response, file, fileList){
      console.log(response);
      if(response.statusCode==="200"){
        this.$message.success('上传成功');
        switch(this.dialog.otherUpDataData.type){
          case 1:
            this.$refs.drawingUp.clearFiles();
            this.$refs.drawing.getTableList()
            break;
          case 2:
            this.$refs.technologyUp.clearFiles();
            this.$refs.technology.getTableList();
            break;
          case 3:
            this.$refs.graphicsFileUp.clearFiles();
            this.$refs.graphicsFile.getTableList();
            break;
        }
      }else{
         this.$message.error(response.desc);
      }
    },
    /**关闭弹框*/ 
    closeDialog(){
      this.status!=='1'?this.getTablList():'';
    },
    /**显示Excel弹框*/ 
    showExcelDialog(){
      let self = this;
      self.dialog4.isShow= true;
      self.dialog4.title = '导入Excel文件';
    }
  }
}
</script>
<style lang="stylus" scoped>
  .addRole
    padding-bottom 10px
  .content-rowspan div
    padding-left: 13px;
    line-height: 46px;
    border-bottom: 1px solid #ECEDEE;
    &:last-child
       border-bottom:none
</style>
